import hashlib
import time

import requests

sess =requests.session()
IP="192.168.1.231"
#银行登录接口（cookie）
def cookie_test():
    #1.发起登录请求
    data={"userName":"admin","password":"1234"}
    rp=sess.request("post","http://"+IP+":8080/pinter/bank/api/login",data=data)
    print(rp.text)
    #2.发起查询请求
    #sess 全局变量帮我们自动处理了cookie
    params ={"userName":"admin"}
    rp = sess.request("get", "http://" + IP + ":8080/pinter/bank/api/query", params=params)
    print(rp.text)
#2.银行登录接口（token）
def token_test():
    #1.登陆后获得权力
    data={"userName":"admin","password":"1234"}
    rp = sess.request("post", "http://" + IP + ":8080/pinter/bank/api/login2", data=data)
    #鉴权接口的，请求头中添加一个属性
    mytoken=rp.json().get("data")
    print(mytoken)
    #2.鉴定他有没有权力
    params={"userName":"admin"}
    headers={"testfan-token":mytoken}
    rp=sess.request("get","http://" + IP + ":8080/pinter/bank/api/query2", params=params,headers=headers)
    print(rp.text)

#3.签名验证
#1.辅助模块：hashlib加密方法
# m=hashlib.md5("123456".encode())
# m.hexdigest()获取加密结果
# time.time() 获取时间戳
#python的时间戳  json的时间戳不一样 (time.time()*1000)

def sign_test():
    mytime=int(time.time()*1000)
    #生成签名认证
    mysign=hashlib.md5(("18091839999"+"testfan"+str(mytime)).encode())
    print(mysign.hexdigest())
    json={"phoneNum":"18091839999","optCode":"testfan","timestamp":mytime,"sign":mysign.hexdigest()}
    rp=sess.request("post","http://"+IP+":8080/pinter/com/userInfo",json=json)
    print(rp.text)

if __name__=='__main__':
    # cookie_test()
    # token_test()
    sign_test()